package com.hopedove.callsystem.activity.call;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.hopedove.base.BaseActivity;
import com.hopedove.callsystem.R;
import com.hopedove.callsystem.constant.ConstantsKt;
import com.hopedove.callsystem.databinding.ActivityScanbarcodeBinding;
import com.hopedove.callsystem.domain.net.GetMaterialInfo;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.TipsDialogFragment;


public class ScanBarcodeActivity extends BaseActivity implements OnClickListener {

    public static String SOURCE_DATA = "source_data";
    private TipsDialogFragment tipDialog;
    private ActivityScanbarcodeBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityScanbarcodeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.barcodeEt.setText("");
        binding.confirmBarcodeImg.setOnClickListener(this);

        // 控制输入框只能接受9为数字，超过9位后，自动覆盖
        binding.barcodeEt.addTextChangedListener(new TextWatcher() {
            private int editEnd;
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                editEnd = binding.barcodeEt.getSelectionEnd();
                if (temp.length() > 11) {
                    s.delete(0, editEnd - 1);
                    binding.barcodeEt.setText(s);
                    binding.barcodeEt.setSelection(editEnd + 1);
                }
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        binding.barcodeEt.setText("");
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm_barcode_img) {
            if (binding.barcodeEt.getText().toString().equals("")) {
                Toast.makeText(this, "请扫描条码", Toast.LENGTH_LONG).show();
                return;
            }
            // 弹出验证条码提示框
            View view = getLayoutInflater().inflate(R.layout.loading, null);
            tipDialog = TipsDialogFragment.newInstance(view);
            TextView textView = view.findViewById(R.id.tip);
            String tipStr = "条码验证中...";
            textView.setText(tipStr);
            tipDialog.setCancelable(false);
            tipDialog.show(getSupportFragmentManager(), "");
            getMaterialInfo();
        }
    }

    /**
     * 获取布料信息
     */
    void getMaterialInfo() {
        Communication.asyncRequest(new GetMaterialInfo(binding.barcodeEt.getText().toString()), r -> {
            TipsDialogFragment.dismissTipDialog(tipDialog);

            if (r instanceof OkResponse) {
                Intent intent = new Intent(ScanBarcodeActivity.this, MaterialInfoActivity.class);
                intent.putExtra(ConstantsKt.getTICKET_NO(), binding.barcodeEt.getText().toString());
                intent.putExtra(SOURCE_DATA, ((OkResponse) r).getData());
                startActivity(intent);
            } else if (r instanceof ErrResponse) {
                binding.barcodeEt.setText(null);
                ToastUtil.longToast(getBaseContext(), ((ErrResponse) r).getErrText());
            }
        });
    }


}
